<template>
  <div class="property">
    <module
      ref="module"
      :moduleType="channelDistribute.moduleType"
      :borderConfig="channelDistribute.borderConfig"
      :moduleConfig="channelDistribute.moduleConfig"></module>
    <module
      ref="module1"
      :moduleType="incomeDistribute.moduleType"
      :borderConfig="incomeDistribute.borderConfig"
      :moduleConfig="incomeDistribute.moduleConfig"></module>
    <module
      ref="module2"
      :borderConfig="incomeMonth.borderConfig"
      :moduleConfig="incomeMonth.moduleConfig"></module>
    <module
      ref="module3"
      :borderConfig="oweMonth.borderConfig"
      :moduleConfig="oweMonth.moduleConfig"></module>
    <module
      ref="module4"
      :borderConfig="collectionRateMonth.borderConfig"
      :moduleConfig="collectionRateMonth.moduleConfig"></module>
    <module
      ref="module5"
      :borderConfig="incomeOrgan.borderConfig"
      :moduleConfig="incomeOrgan.moduleConfig"
      :isShowDimension="incomeOrgan.isShowDimension"></module>
    <module
      ref="module6"
      :borderConfig="oweOrgan.borderConfig"
      :moduleConfig="oweOrgan.moduleConfig"
      :dimensionArr="oweOrgan.dimensionArr"
      :isShowDimension="oweOrgan.isShowDimension"></module>
    <module
      ref="module7"
      :borderConfig="collectionRateOrgan.borderConfig"
      :moduleConfig="collectionRateOrgan.moduleConfig"
      :isShowDimension="collectionRateOrgan.isShowDimension"></module>
  </div>
</template>

<script>
import module from '../components/SecondaryModule'
import { changeBarData, separateData } from './../../utils/index'
export default {
  components: {
    module
  },
  computed: {
    organId () {
      return this.$store.state.organId
    }
  },
  name: 'property',
  data () {
    return {
      // 本月收费渠道配置
      channelDistribute: {
        moduleType: 'pie',
        borderConfig: {
          moduleType: '2x1',
          icon: '',
          titleText: '本月收费渠道',
          ref: 'channelDistribute'
        },
        moduleConfig: {
          optionsConfig: {
            isUseValue: true,
            value: [],
            labelLine: {
              normal: {
                length: 30,
                length2: 20
              }
            },
            legend: []
          },
          legendList: [],
          legendDirection: 'vertical'
        }
      },
      // 本月实收科目配置
      incomeDistribute: {
        moduleType: 'pie',
        borderConfig: {
          moduleType: '2x1',
          icon: '',
          titleText: '本月实收科目',
          ref: 'incomeDistribute'
        },
        moduleConfig: {
          optionsConfig: {
            isUseValue: true,
            labelLine: {
              normal: {
                length: 30,
                length2: 20
              }
            },
            value: [],
            legend: [],
            tooltipsUnit: '万元'
          },
          legendList: [],
          legendDirection: 'vertical'
        }
      },
      // 实收金额趋势配置
      incomeMonth: {
        borderConfig: {
          titleText: '实收金额趋势',
          moduleType: '43x1',
          ref: 'incomeMonth'
        },
        moduleConfig: {
          optionsConfig: {
            title: {
              text: '万元',
              textStyle: {
                color: '#7aabe7'
              },
              top: 5,
              left: 30
            },
            axis: {
              yAxisConversion: 'w',
              xAxisDataType: 2 // X轴坐标从1月份开始
            },
            yAxis: {
              minInterval: 10
            },
            tooltipsConfig: {
              symbol: '万元',
              isNamePreposition: false,
              tipsMessage: '实收'
            },
            grid: {
              top: 40
            },
            seriesData: []
          },
          legendDirection: 'level',
          legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 0 }, { name: new Date().getFullYear(), colorIndex: 1 }]
        }
      },
      // 欠费金额趋势
      oweMonth: {
        borderConfig: {
          titleText: '欠费金额趋势',
          moduleType: '43x1',
          ref: 'oweMonth'
        },
        moduleConfig: {
          optionsConfig: {
            title: {
              text: '万元',
              textStyle: {
                color: '#7aabe7'
              },
              top: 5,
              left: 30
            },
            axis: {
              yAxisConversion: 'w',
              xAxisDataType: 2 // X轴坐标从1月份开始
            },
            yAxis: {
              minInterval: 10
            },
            tooltipsConfig: {
              symbol: '万元',
              isNamePreposition: false,
              tipsMessage: '欠费',
              tipsColor: ['#2e92cb', '#f29f46']
            },
            grid: {
              top: 40
            },
            seriesData: []
          },
          legendDirection: 'level',
          legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 0 }, { name: new Date().getFullYear(), colorIndex: 2 }]
        }
      },
      // 收缴率趋势
      collectionRateMonth: {
        borderConfig: {
          titleText: '收缴率趋势',
          moduleType: '43x1',
          ref: 'collectionRateMonth'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              yAxisConversion: '%', // Y轴坐标%
              xAxisDataType: 2 // X轴坐标从1月份开始
            },
            yAxis: {
              maxValue: 100
            },
            tooltipsConfig: {
              symbol: '%',
              isNamePreposition: false,
              tipsMessage: '收缴率'
            },
            grid: {
              top: 40
            },
            seriesData: []
          },
          legendDirection: 'level',
          legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 0 }, { name: new Date().getFullYear(), colorIndex: 1 }]
        }
      },
      // 实收金额对比
      incomeOrgan: {
        borderConfig: {
          titleText: '实收金额对比',
          moduleType: '43x1',
          ref: 'incomeOrgan'
        },
        moduleConfig: {
          optionsConfig: {
            title: {
              text: '万元',
              textStyle: {
                color: '#7aabe7'
              },
              top: 5,
              left: 30
            },
            axis: {
              xAxisNameLength: 8,
              yAxisConversion: 'w'
            },
            xAxis: {
              axisLabel: {
                rotate: 30
              },
              data: []
            },
            yAxis: {
              minInterval: 10
            },
            tooltipsConfig: {
              symbol: '万元',
              isNamePreposition: true
            },
            grid: {
              top: 40,
              bottom: 0
            },
            dataZoom: {
              show: true,
              left: '50%'
            },
            seriesData: []
          }
        },
        isShowDimension: true
      },
      // 欠费金额对比
      oweOrgan: {
        borderConfig: {
          titleText: '欠费金额对比',
          moduleType: '43x1',
          ref: 'oweOrgan'
        },
        moduleConfig: {
          optionsConfig: {
            title: {
              text: '万元',
              textStyle: {
                color: '#7aabe7'
              },
              top: 5,
              left: 30
            },
            axis: {
              xAxisNameLength: 8,
              yAxisConversion: 'w' // Y轴坐标%
            },
            xAxis: {
              axisLabel: {
                rotate: 30
              },
              data: []
            },
            yAxis: {
              minInterval: 10
            },
            tooltipsConfig: {
              symbol: '万元',
              tipsColor: ['#b60d81'],
              isNamePreposition: true
            },
            grid: {
              top: 40,
              bottom: 0
            },
            dataZoom: {
              show: true,
              left: '50%'
            },
            seriesData: []
          }
        },
        isShowDimension: true,
        dimensionArr: ['本月', '合计']
      },
      // 收缴率对比
      collectionRateOrgan: {
        borderConfig: {
          titleText: '收缴率对比',
          moduleType: '43x1',
          ref: 'collectionRateOrgan'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              xAxisNameLength: 8,
              yAxisConversion: '%' // Y轴坐标%
            },
            xAxis: {
              axisLabel: {
                rotate: 30
              },
              data: []
            },
            yAxis: {
              max: 100
            },
            tooltipsConfig: {
              symbol: '%',
              tipsColor: ['#f29f46'],
              isNamePreposition: true
            },
            grid: {
              top: 40,
              bottom: 0
            },
            dataZoom: {
              show: true,
              left: '50%'
            },
            seriesData: []
          }
        },
        isShowDimension: true
      }
    }
  },
  methods: {
    showTips (data) {
      this.$parent.showTips(data)
    },
    // 改变时间维度时
    changeDimension (obj) {
      switch (obj.ref) {
        case 'incomeOrgan': this.getBranchPropertyFee(obj.index + 1)
          break
        case 'oweOrgan': this.getBranchPropertyArrearage(obj.index + 1)
          break
        case 'collectionRateOrgan': this.getBranchPayRate(obj.index + 1)
          break
      }
    },
    // 本月收费渠道
    getPropertyFeeChannel () {
      this.propertyApi.getPropertyFeeChannel({ organId: this.organId }).then(res => {
        let value = []
        let legend = []
        let legendList = []
        let data = []
        if (res.data.code === '0') {
          data = res.data.data
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        data.forEach((item, index) => {
          value.push(item.fee)
          legend.push(item.channelTypeName)
          legendList.push(item.channelTypeName)
        })
        if (legendList.length > 8) {
          legendList.splice(7, legendList.length - 7, '其余类型')
        }
        this.channelDistribute.moduleConfig.optionsConfig.value = value
        this.channelDistribute.moduleConfig.optionsConfig.legend = legend
        this.channelDistribute.moduleConfig.legendList = legendList
      }, res => {
        this.channelDistribute.moduleConfig.optionsConfig.value = []
        this.channelDistribute.moduleConfig.optionsConfig.legend = []
        this.channelDistribute.moduleConfig.legendList = []
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 本月实收科目
    getPropertyFeeDistribution () {
      this.propertyApi.getPropertyFeeDistribution({ organId: this.organId }).then(res => {
        let value = []
        let legend = []
        let legendList = []
        let data = []
        if (res.data.code === '0') {
          data = res.data.data
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        data.forEach((item, index) => {
          value.push(item.fee)
          legend.push(item.acctItemTypeName)
          legendList.push(item.acctItemTypeName)
        })
        if (legendList.length > 8) {
          legendList.splice(7, legendList.length - 7, '其余类型')
        }
        this.incomeDistribute.moduleConfig.optionsConfig.value = value
        this.incomeDistribute.moduleConfig.optionsConfig.legend = legend
        this.incomeDistribute.moduleConfig.legendList = legendList
      }, res => {
        this.incomeDistribute.moduleConfig.optionsConfig.value = []
        this.incomeDistribute.moduleConfig.optionsConfig.legend = []
        this.incomeDistribute.moduleConfig.legendList = []
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 实收金额趋势
    getPropertyFeeYearOnYear () {
      this.propertyApi.getPropertyFeeYearOnYear({ organId: this.organId }).then(res => {
        let data = [[], []]
        if (res.data.code === '0') {
          data = changeBarData(res.data.data, 'fee')
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.incomeMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: data[0],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0,
            isBar: true
          },
          {
            data: data[1],
            name: new Date().getFullYear() + '年',
            colorIndex: 1,
            isBar: true
          }
        ]
      }, res => {
        this.incomeMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0,
            isBar: true
          },
          {
            data: [],
            name: new Date().getFullYear() + '年',
            colorIndex: 1,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 欠费金额趋势
    getPropertArrearageYearOnYear () {
      this.propertyApi.getPropertArrearageYearOnYear({ organId: this.organId }).then(res => {
        let data = [[], []]
        if (res.data.code === '0') {
          data = changeBarData(res.data.data, 'arrearage')
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.oweMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: data[0],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0,
            isBar: true
          },
          {
            data: data[1],
            name: new Date().getFullYear() + '年',
            colorIndex: 2,
            isBar: true
          }
        ]
      }, res => {
        this.oweMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0,
            isBar: true
          },
          {
            data: [],
            name: new Date().getFullYear() + '年',
            colorIndex: 2,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 收缴率趋势
    getPropertPayRateYearOnYear () {
      this.propertyApi.getPropertPayRateYearOnYear({ organId: this.organId }).then(res => {
        let data = [[], []]
        if (res.data.code === '0') {
          data = changeBarData(res.data.data, 'payRate')
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.collectionRateMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: data[0],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0
          },
          {
            data: data[1],
            name: new Date().getFullYear() + '年',
            colorIndex: 1
          }
        ]
      }, res => {
        this.collectionRateMonth.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: new Date().getFullYear() - 1 + '年',
            colorIndex: 0
          },
          {
            data: [],
            name: new Date().getFullYear() + '年',
            colorIndex: 1
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 实收金额对比
    getBranchPropertyFee (timeDimensionIndex) {
      let timeDimension = 1
      if (timeDimensionIndex) {
        timeDimension = timeDimensionIndex
      }
      this.propertyApi.getBranchPropertyFee({ organId: this.organId, timeDimension: timeDimension }).then(res => {
        let data = []
        let name = []
        if (res.data.code === '0') {
          let obj = separateData(res.data.data, 'fee')
          data = obj.data
          name = obj.tipsName
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.incomeOrgan.moduleConfig.optionsConfig.xAxis.data = name
        this.incomeOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: data,
            name: '实收',
            colorIndex: 0,
            isBar: true
          }
        ]
      }, res => {
        this.incomeOrgan.moduleConfig.optionsConfig.xAxis.data = []
        this.incomeOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: '实收',
            colorIndex: 0,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 欠费金额对比
    getBranchPropertyArrearage (timeDimensionIndex) {
      let timeDimension = 1
      if (timeDimensionIndex) {
        timeDimension = timeDimensionIndex
      }
      this.propertyApi.getBranchPropertyArrearage({ organId: this.organId, timeDimension: timeDimension }).then(res => {
        let data = []
        let name = []
        if (res.data.code === '0') {
          let obj = separateData(res.data.data, 'arrearage')
          data = obj.data
          name = obj.tipsName
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.oweOrgan.moduleConfig.optionsConfig.xAxis.data = name
        this.oweOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: data,
            name: '欠费',
            colorIndex: 1,
            isBar: true
          }
        ]
      }, res => {
        this.oweOrgan.moduleConfig.optionsConfig.xAxis.data = []
        this.oweOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: '欠费',
            colorIndex: 1,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 收缴率对比
    getBranchPayRate (timeDimensionIndex) {
      let timeDimension = 1
      if (timeDimensionIndex) {
        timeDimension = timeDimensionIndex
      }
      this.propertyApi.getBranchPayRate({ organId: this.organId, timeDimension: timeDimension }).then(res => {
        let data = []
        let name = []
        if (res.data.code === '0') {
          let obj = separateData(res.data.data, 'payRate')
          data = obj.data
          name = obj.tipsName
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        this.collectionRateOrgan.moduleConfig.optionsConfig.xAxis.data = name
        this.collectionRateOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: data,
            name: '收缴率',
            colorIndex: 2,
            isBar: true
          }
        ]
      }, res => {
        this.collectionRateOrgan.moduleConfig.optionsConfig.xAxis.data = []
        this.collectionRateOrgan.moduleConfig.optionsConfig.seriesData = [
          {
            data: [],
            name: '收缴率',
            colorIndex: 2,
            isBar: true
          }
        ]
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 请求数据
    getData () {
      this.getPropertyFeeChannel()
      this.getPropertyFeeDistribution()
      this.getPropertyFeeYearOnYear()
      this.getPropertArrearageYearOnYear()
      this.getPropertPayRateYearOnYear()
      this.getBranchPropertyFee()
      this.getBranchPropertyArrearage()
      this.getBranchPayRate()
    },
    resizeChart () {
      this.$refs.module.resizeChart()
      this.$refs.module1.resizeChart()
      this.$refs.module2.resizeChart()
      this.$refs.module3.resizeChart()
      this.$refs.module4.resizeChart()
      this.$refs.module5.resizeChart()
      this.$refs.module6.resizeChart()
      this.$refs.module7.resizeChart()
    }
  },
  mounted () {
    // this.getData()
  }
}
</script>

<style lang="scss" scoped>
  .property{
    width: calc(100% - 10px);
    position: relative;
    margin: 5px;
    overflow: hidden;
    moz-user-select: -moz-none;
    -moz-user-select: none;
    -o-user-select:none;
    -khtml-user-select:none;
    -webkit-user-select:none;
    -ms-user-select:none;
    user-select:none;
  }
</style>
